package oop.core.control;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import oop.core.view.ControllerConfigMenu;

public class ControlConfigSaver {

	public void saveConfig(ControllerConfigMenu menu)
	{
		Map<Character, Integer> config = menu.getNewConfig();
		
		JFileChooser jFileChooser = new JFileChooser();
		jFileChooser.showOpenDialog(null);
		File file = jFileChooser.getSelectedFile();
		
		if(!menu.isValidConfig())
		{
			JOptionPane.showMessageDialog(null, "Configuration is not valid.");
			return;
		}
		
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(file);
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "Error saving configuration file.");
			return;
		}
		
		for(Character c : config.keySet())
		{
			pw.println(c + "#" + config.get(c));
		}
		pw.flush();
		JOptionPane.showMessageDialog(null, "Configuration saved successfully.");
	}
}
